import toolBar from './toolbar'
import {
  AddNodeCommand,
  RemoveNodeCommand,
  ChangeNodeCommand,
  AddInduceCommand,
  RemoveInduceCommand,
  ChangeInduceCommand,
  AddWireFrameCommand,
  RemoveWireFrameCommand
} from './command'
import Node from '../Node'
import editor from './editor'
import Induce from '../assist/induce2'
import WireFrame from '../assist/wireFrame'

var mindEvent = {
  init(zr, stack, mind) {
    this.zr = zr;
    this.stack = stack;
    this.mind = mind;
    this.initEvent();
    this.mouseEvent();
    editor.init();
  },
  initEvent() {
    this.selectNode = null;
    this.selectInduce = null;
    this.zr.on('click', (e) => {
      //点击空白处
      if (!e.target) {
        this.zr.trigger('clearSelectNode');
      }
    });
    //编辑节点
    this.zr.on('editNode', (e) => {
      var node = e.node;
      editor.node = node;
      var pos = node.getPosition();
      var box = node.getBoundingRect();
      var minderPos = node.getMinder().getPosition();
      var text = node.getData('text');
      var offset = $('#mindEditor').offset();
      //如果放大缩小，需要调整为正常状态，这里实现不太好，需要优化
      // if (this.mind.scale[0] != 1) {
      //   this.mind.attr({
      //     scale: [1, 1]
      //   })
      // }

      var caleX=this.mind.scale[0];
      var caleY=this.mind.scale[1];

      editor.type = 'editNode';
      editor.setPosition(pos.x*caleX + minderPos.x + offset.left, pos.y*caleY + minderPos.y + offset.top).show();
      editor.width(box.width*caleX);
      editor.height(box.height*caleY);
      editor.setValue(text);
    });
    //选中节点
    this.zr.on('selectNode', (e) => {
      toolBar.selectNode(e.node);
      this.selectNode = e.node;
    });
    //取消选中
    this.zr.on('clearSelectNode', (e) => {
      toolBar.clearSelectNode();
      this.selectNode && this.selectNode.getMinder().clearSelectNode();
      this.selectNode = null;
    });
    //添加同级节点
    this.zr.on('addNode', () => { //添加同级节点
      var newNode = new Node({
        id: new Date().getTime(),
        text: '同级节点'
      });
      this.selectNode.getParent() && this.stack.execute(new AddNodeCommand(newNode, this.selectNode.getParent()));
      toolBar.view();
    });
    //添加子节点
    this.zr.on('addChild', () => {
      var newNode = new Node({
        id: new Date().getTime(),
        text: '子节点'
      });
      this.stack.execute(new AddNodeCommand(newNode, this.selectNode));
      toolBar.view();
    });
    //移除子节点
    this.zr.on('delete', () => {
      if (this.selectNode.getLevel() != 0) {
        this.stack.execute(new RemoveNodeCommand(this.selectNode, this.selectNode.getParent()));
        this.selectNode.unSelect();
        this.zr.trigger('clearSelectNode');
        toolBar.view();
      }
    });
    //加粗
    this.zr.on('bold', () => {
      var oldData = this.selectNode.getData();
      if (oldData.fontWeight == 'bold') {
        var newData = Object.assign({}, oldData, {
          fontWeight: ''
        })
        this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      } else {
        var newData = Object.assign({}, oldData, {
          fontWeight: 'bold'
        })
        this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      }
      toolBar.view();
    });
    //倾斜
    this.zr.on('italic', () => {
      var oldData = this.selectNode.getData();
      if (oldData.fontStyle == 'italic') {
        var newData = Object.assign({}, oldData, {
          fontStyle: ''
        })
        this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      } else {
        var newData = Object.assign({}, oldData, {
          fontStyle: 'italic'
        });
        this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      }
      toolBar.view();
    });
    //大小
    this.zr.on('fontSize', (e) => {
      var oldData = this.selectNode.getData();
      var newData = Object.assign({}, oldData, {
        fontSize: e.value || 12
      });
      this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      toolBar.view();
    });
    //字体
    this.zr.on('fontFamily', (e) => {
      var oldData = this.selectNode.getData();
      var newData = Object.assign({}, oldData, {
        fontFamily: e.value
      });
      this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      toolBar.view();
    });
    //字体颜色
    this.zr.on('textFill', (e) => {
      if (this.selectNode) {
        var oldData = this.selectNode.getData();
        var newData = Object.assign({}, oldData, {
          textFill: e.value
        });
        this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
        toolBar.view();
      }
    });
    //放大缩小
    this.zr.on('zoomIn', () => {
      this.mind.zoom('up');
    });
    this.zr.on('zoomOut', () => {
      this.mind.zoom('down');
    });

    //添加归纳
    this.zr.on('induce', () => {
      if (!this.selectNode.induce) {
        var induce = new Induce(this.selectNode, '归纳信息');
        this.stack.execute(new AddInduceCommand(this.selectNode, induce));
        toolBar.view();
      }
    });

    this.zr.on('removeInduce', () => {
      //this.stack.execute(new RemoveInduceCommand(this.selectNode,induce));
      toolBar.view();
    });

    //编辑归纳
    this.zr.on('editInduce', (e) => {
      this.selectInduce = e.induce;
      var text = this.selectInduce.getText();
      editor.type = 'editInduce';
      editor.induce = e.induce;
      var pos = e.induce.getPosition();
      var textbox = e.induce.getTextBox();
      var box = e.induce.getBoundingRect();
      var minderPos = e.induce.node.getMinder().getPosition();
      var offset = $('#mindEditor').offset();
      var caleX=this.mind.scale[0];
      var caleY=this.mind.scale[1];

      var p = 0;
      if (e.induce.type == 'right') {
        p = 16*caleY;
      }
     
      editor.setValue(text).setPosition(pos.x*caleX + minderPos.x + offset.left + p-1, pos.y*caleY + minderPos.y + offset.top + (box.height*caleY - textbox.height*caleY) / 2 - 5);
      editor.width(textbox.width*caleX).height(textbox.height*caleY);
      editor.show();
    });

    //设置优先级
    this.zr.on('priority', (e) => {
      var priority = e.value;
      var oldData = this.selectNode.getData();
      if (!oldData.hasOwnProperty('priority')) {
        oldData.priority = '';
      }
      var newData = {
        ...oldData,
        ...{
          priority: priority
        }
      };
      this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      toolBar.view();

    });
    this.zr.on('removePriority', () => {
      var oldData = this.selectNode.getData();
      var newData = {
        ...oldData,
        ...{
          priority: ''
        }
      };
      this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
      toolBar.view();
    });

    this.zr.on('expandNode',(e)=>{
      var node=e.node;
      node.expand();
      node.getMinder().refresh();
    });

    this.zr.on('collapseNode',(e)=>{
      var node=e.node;
      node.collapse();
      node.getMinder().refresh();
    });


    //添加线框
    this.zr.on('wireFrame',()=>{
      if (!this.selectNode.wireFrame) {
        var wf = new WireFrame(this.selectNode);
        this.stack.execute(new AddWireFrameCommand(this.selectNode, wf));
        toolBar.view();
      }
    });

    //移除线框
    this.zr.on('removeWireFrame',()=>{

    });

    this.zr.on('undo', () => {
      this.stack.undo();
      toolBar.view();
    });

    this.zr.on('redo', () => {
      this.stack.redo();
      toolBar.view();
    });
  },
  mouseEvent() {
    var drag = false;
    var startX = 0;
    var startY = 0;
    var position = null;
    this.zr.on('mousedown', (e) => {
      //如果处以编辑状态
      this.needEditor();
      drag = true;
      startX = e.offsetX;
      startY = e.offsetY;
      position = this.mind.getPosition();
    })
    this.zr.on('mousemove', (e) => {
      if (drag) {
        var dx = e.offsetX - startX;
        var dy = e.offsetY - startY;
        this.mind.setPosition(position.x + dx, position.y + dy);
      }
    })
    this.zr.on('mouseup', () => {
      drag = false;
      var startX = 0;
      var startY = 0;
      var position = null;
    });
  },
  needEditor() {
    if (editor.isShow()) {
      //只有修改了文字才会生成命令
      if (editor.type == "editNode") { //编辑节点
        if (editor.getValue() != editor.node.getData('text')) {
          let oldData = editor.node.getData();
          let newData = Object.assign({}, oldData, {
            text: editor.getValue()
          });
          this.stack.execute(new ChangeNodeCommand(this.selectNode, oldData, newData));
          toolBar.view();
        }
      } else { //编辑总结
        if (editor.getValue() != editor.induce.getText()) {
          let oldData = editor.induce.getData();
          let newData = Object.assign({}, oldData, {
            text: editor.getValue()
          });
          this.stack.execute(new ChangeInduceCommand(editor.induce, oldData.text, newData.text));
          toolBar.view();
        }
      }
      editor.hide();
      editor.setValue('');
    }
  }
};

export default mindEvent;